package org.sagemath.commandline;

import java.io.Console;
import java.util.LinkedList;
import java.util.ListIterator;

import javax.swing.SingleSelectionModel;

import org.sagemath.singlecellserver.CommandOutput;
import org.sagemath.singlecellserver.CommandReply;
import org.sagemath.singlecellserver.ExecuteRequest;
import org.sagemath.singlecellserver.Interact;
import org.sagemath.singlecellserver.SageSingleCell;
import org.sagemath.singlecellserver.SageSingleCell.LogLevel;
import org.sagemath.singlecellserver.SageSingleCell.ServerTask;

import junit.framework.Assert;

public class Test {
	private static final String TAG = "Client";

	private SageSingleCell server = new SageSingleCell();
	
	public Test() {
		//	server.setServer("http://sagemath.org:5467", "/eval", "/output_poll", "/files");
		server.setServer("http://aleph.sagemath.org", "/eval", "/output_poll", "/files");
		server.setDownloadDataFiles(false);
		server.setLogLevel(LogLevel.BRIEF);
	}

	public static void main(String[] args) {
		Test test = new Test();
		test.run();
	}
	
	private void run() {
		//test("plot(sin(x),x,0,1)").run();
		
		if (false)
		test("1+1").run();
		

		if (false)
	    test("@interact\n" + 
	    	 "def f(n=Selector(values=[\"Option1\",\"Option2\"])):\n" +
	         "    print n")
	         .addInteract("n", 1)
			 .run();
   


		
		if (false)
		test("@interact\n" +
			 "def f(n=DiscreteSlider(values=[2,3,5,7,11,13])):\n" +
			 "    print n*n")
			 .addInteract("n", 2)
			 .run();
		

		
		if (false)
		test("@interact\n" +
			 "def f(n=(1..10)):\n" +
			 "    print n*n")
			 .addInteract("n", 7)
			 .run();
		
		if (false)
		test("1+1").run();
		
	
//		if (false)
		test("@interact\n" +
				"def f(n = ContinuousSlider()):\n" +
				"    html(n)\n" +
				"    print n\n" +
				"    plot(sin(x), x, 0, 2*pi).show()")
			.addInteract("n", 42)
			.addInteract("n", 13)
			.run();
		
		
//		run("help(plot)");
//		run("1+1");
//		run("sin(0.5)");
//		run("a + b");
//		run("html(1/2)");
//		run("plot(sin(x),x,0,1)");
	}

	private Testcase test(String command) {
		Testcase testcase = new Testcase(server, command);
		server.setOnSageListener(testcase);
		return testcase;
	}

//	private void run(String command) {
//		runSerial(command);
//	}
	
	
//	private void runSerial(String command) {
//		ExecuteRequest request = new ExecuteRequest(command);
//		System.out.println(request);
//		LinkedList<CommandReply> reply = request.receiveReply(server.new ServerTask());
//		ListIterator<CommandReply> iter = reply.listIterator();
//		while (iter.hasNext())
//			System.out.println(iter.next());
//		System.out.println();
//	}
//
//	private void runParallel(String command) {
//		System.out.println("\nRunning "+command);
//		server.query(command);
//	}
//
//	
//	@Override
//	public void onSageOutputListener(CommandOutput output) {
//		System.out.println("SageOutput");
//		output.prettyPrint();
//	}
//	
//	@Override
//	public void onSageReplaceOutputListener(CommandOutput output) {
//		System.out.println("SageReplaceOutput");
//		output.prettyPrint();
//	}
//	
//	static int interactChanges = 2;
//	
//	@Override
//	public void onSageInteractListener(Interact interact) {
//		// interact.prettyPrint();
//		if (interactChanges-- <= 0) return;
//		System.out.println("changing interact");
//		server.interact(interact, "n", 42);
//	}
}
